﻿package frEngine.event
{
    import flash.utils.*;

    public class FrEventDispatcher extends Object
    {
        private var _eventMap:Dictionary;
        private const _eventTypeList:Dictionary;
        public static const instance:FrEventDispatcher = new FrEventDispatcher;

        public function FrEventDispatcher()
        {
            this._eventMap = new Dictionary(false);
            this._eventTypeList = new Dictionary(false);
            return;
        }// end function

        public function proxyDispatchEvent(param1:Object, param2:String, param3:Array = null) : Boolean
        {
            if (!param1)
            {
                return false;
            }
            if (this.hasListioner(param1, param2) == false)
            {
                return false;
            }
            var _loc_4:* = this._eventTypeList[param2];
            if (!_loc_4)
            {
                var _loc_6:* = new ParamsEvent(param2, param3, false);
                _loc_4 = new ParamsEvent(param2, param3, false);
                this._eventTypeList[param2] = _loc_6;
            }
            else
            {
                _loc_4.params = param3;
            }
            var _loc_5:* = param1.dispatchEvent(_loc_4);
            _loc_4.params = null;
            return _loc_5;
        }// end function

        public function proxyRemoveEventListener(param1:Object, param2:String, param3:Function, param4:Boolean = false) : void
        {
            var _loc_8:Boolean = false;
            var _loc_9:String = null;
            if (!param1)
            {
                return;
            }
            if (this.hasListioner(param1, param2) == false)
            {
                return;
            }
            var _loc_5:* = this._eventMap[param1];
            var _loc_6:* = _loc_5[param2];
            var _loc_7:* = _loc_6.indexOf(param3);
            if (_loc_7 < 0)
            {
                return;
            }
            _loc_6.splice(_loc_7, 1);
            param1.removeEventListener(param2, param3);
            if (_loc_6.length == 0)
            {
                delete _loc_5[param2];
                _loc_8 = true;
                for (_loc_9 in _loc_5)
                {
                    
                    _loc_8 = false;
                    break;
                }
                if (_loc_8)
                {
                    delete this._eventMap[param1];
                }
            }
            return;
        }// end function

        public function removeAllProxyEventListener(param1:Object) : void
        {
            var _loc_3:String = null;
            var _loc_4:Array = null;
            var _loc_5:int = 0;
            var _loc_6:int = 0;
            var _loc_2:* = this._eventMap[param1];
            if (!_loc_2)
            {
                return;
            }
            for (_loc_3 in _loc_2)
            {
                
                _loc_4 = _loc_2[_loc_3];
                _loc_5 = _loc_4.length;
                _loc_6 = 0;
                while (_loc_6 < _loc_5)
                {
                    
                    param1.removeEventListener(_loc_3, _loc_4[_loc_6]);
                    _loc_6++;
                }
                delete _loc_2[_loc_3];
            }
            delete this._eventMap[param1];
            return;
        }// end function

        public function proxyAddEventListener(param1:Object, param2:String, param3:Function, param4:Boolean = false, param5:int = 0, param6:Boolean = false) : void
        {
            if (!param1)
            {
                return;
            }
            var _loc_7:* = this._eventMap[param1];
            if (!_loc_7)
            {
                var _loc_10:* = new Dictionary(false);
                this._eventMap[param1] = new Dictionary(false);
                _loc_7 = _loc_10;
            }
            var _loc_8:* = _loc_7[param2];
            var _loc_9:Boolean = false;
            if (!_loc_8)
            {
                var _loc_10:* = new Array();
                _loc_7[param2] = new Array();
                _loc_8 = _loc_10;
                _loc_9 = true;
            }
            else if (_loc_8.indexOf(param3) == -1)
            {
                _loc_9 = true;
            }
            if (_loc_9)
            {
                _loc_8.push(param3);
                param1.addEventListener(param2, param3, param4, param5, param6);
            }
            return;
        }// end function

        public function hasListioner(param1:Object, param2:String) : Boolean
        {
            var _loc_3:* = this._eventMap[param1];
            if (!_loc_3)
            {
                return false;
            }
            var _loc_4:* = _loc_3[param2];
            if (!_loc_4)
            {
                return false;
            }
            return true;
        }// end function

    }
}
